<?php
class date_timezone
{

	/**
	 * 取指定时区到Etc/GMT+0的偏差的秒数
	 * 
	 * @param $timezone string       	
	 */
	static function get_offset ($timezone = NULL)
	{
		if ($timezone === NULL) $timezone = date_default_timezone_get();
		$date_time_zone = new DateTimeZone($timezone);
		$date_time = new DateTime("now", $date_time_zone);
		return $date_time_zone->getOffset($date_time);
	}

	/**
	 * 取时指定区到Etc/GMT+0的偏差的小时数
	 * America/New_York -5
	 * America/Los_Angeles -8
	 * Asia/Tehran 3.5
	 *
	 * @param $timezone string       	
	 */
	static function get_offset_hours ($timezone = NULL ,$human_format = FALSE)
	{
		$seconds = self::get_offset($timezone);
		$hours = $seconds / 60 / 60;
		if (! $human_format) return $hours;
		$ret = $hours < 0 ? "" : '+';
		if (is_float($hours))
		{
			$hour = floor($hours);
			$minutes = fmod($hours, 1) * 60;
			$ret .= "{$hour}:{$minutes}";
		}
		else
		{
			$ret .= "{$hours}:00";
		}
		return $ret;
	}
	/**
	 * 将指定时区时间转换为另一时区的时间，如果指定时区为空则为当前默认时区
	 * @param string $str_time
	 * @param string $timezone
	 * @param string $to_timezone
	 */
	static function get_from_time($str_time,$timezone,$to_timezone=NULL)
	{
			$dateTime = new DateTime($str_time, new DateTimeZone($timezone));
			if(!$to_timezone)$to_timezone=date_default_timezone_get();
			return $dateTime->setTimezone(new DateTimeZone($to_timezone))->getTimestamp();
	}
}